<template>
	<main-container>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button type="primary" v-auth="'upms:systenant:add'" @click="add"
					><el-icon> <ele-Plus /> </el-icon>新增</el-button
				>
			</div>
			<right-toolbar :searchBtn="false" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<Form ref="refForm" @initPage="initPage"></Form>
		<TenantMenu ref="refTenantMenu" @initPage="initPage"></TenantMenu>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="tableData" border>
			<el-table-column prop="name" label="租户名" />
			<el-table-column prop="siteUrl" label="官网" />
			<el-table-column prop="phone" label="手机号" />
			<el-table-column prop="authBeginTime" label="授权开始时间" />
			<el-table-column prop="authEndTime" label="授权结束时间" />
			<el-table-column prop="status" label="状态">
				<template #default="scope">
					<el-tag v-if="scope.row.status == '0'" type="danger">停用</el-tag>
					<el-tag v-if="scope.row.status == '1'" type="success">正常</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" />
			<el-table-column label="操作" width="220" align="center">
				<template #default="scope">
					<el-button link type="primary" v-auth="'upms:systenant:edit'" @click="edit(scope.row)"
						><el-icon> <ele-Edit /> </el-icon>修改</el-button
					>
					<el-button link type="primary" v-auth="'upms:systenant:add'" @click="upMenu(scope.row.id)">
						<el-icon> <ele-Plus /> </el-icon>配置菜单
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="page.total"
			v-model:currentPage="page.currentPage"
			v-model:pageSize="page.pageSize"
			@changePage="initPage"
		></pagination>
	</main-container>
</template>
<script lang="ts">
import { defineComponent, defineAsyncComponent, ref, reactive, toRefs } from 'vue';
import { getPage } from '/@/api/upms/tenant';
export default defineComponent({
	components: { Form: defineAsyncComponent(() => import('./form.vue')), TenantMenu: defineAsyncComponent(() => import('./tenantmenu.vue')) },
	setup() {
		const data = reactive({
			queryParams: {},
			page: {
				total: 0,
				currentPage: 1,
				pageSize: 10,
				ascs: [],
				descs: 'create_time',
			},
			tableData: [],
		});
		const showSearch = ref(true);
		const loading = ref(false);
		const refForm = ref();
		const refTenantMenu = ref();

		const initPage = async () => {
			loading.value = true;
			let params = {
				current: data.page.currentPage,
				size: data.page.pageSize,
				ascs: data.page.ascs,
				descs: data.page.descs,
			};
			await getPage(Object.assign(params, data.queryParams))
				.then((response) => {
					data.tableData = response.data.data.records;
					data.page.total = response.data.data.total;
					loading.value = false;
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 新增按钮
		 */
		const add = () => {
			refForm.value.initForm();
		};
		/**
		 * 修改按钮
		 */
		const edit = (row: any) => {
			refForm.value.initForm(row);
		};
		initPage();
		/**
		 * 重置搜索表单
		 */
		const resetQuery = () => {
			data.queryParams = {};
		};
		const upMenu = (id: string) => {
			refTenantMenu.value.initMenu(id);
		};
		return {
			loading,
			refForm,
			showSearch,
			refTenantMenu,
			add,
			edit,
			upMenu,
			initPage,
			resetQuery,
			...toRefs(data),
		};
	},
});
</script>
